package ru.alexo.whiskey.events;

import java.io.IOException;
import java.io.OutputStream;

public class TempoChangeEvent implements TrackEvent {

    private static final int MPM = 60000000;

    private final int tempo;

    public TempoChangeEvent(int tempo) {
	super();
	this.tempo = tempo;
    }

    public int serializeToStream(OutputStream dos, int offset) throws IOException {
	int tmp = MPM / tempo;

	int[] bytes = new int[3];
	for (int i = 2; i >= 0; i--) {
	    bytes[i] = tmp & 0x000000FF;
	    tmp >>= 8;
	}

	dos.write(0x00);
	dos.write(0xFF);
	dos.write(0x51);
	dos.write(0x03);
	for (int byteValue : bytes) {
	    dos.write(byteValue);
	}

	return 0;
    }
}
